import {Redirect, Route, Switch} from "react-router-dom"
import {mainRoutes} from "./router"
import {connect} from "react-redux"
import Main from "./components/Main"

function App(props) {
  
  let {isLogin}=props.userReducer

  return (
    <div>
      <Switch >
        {/* 第一层路由控制 */}

        <Route path="/admin" render={()=>{
          if (isLogin) {
            return <Main />
          }else{
            return <Redirect to="/login" />
          }
        }} />

        { 
          mainRoutes.map((item,index)=>{
            return <Route path={item.path} component={item.component} key={index} />
          })
        }

        <Redirect path="/" exact to="/admin" />
        <Redirect path="*" to="/404" />
      </Switch>
    </div>
  );
}

const mapStateToProps =(state)=>{
  return state
}
export default connect(mapStateToProps)(App);
